import time
import numpy as np

import base_api
import operation


def readWriteTest():
    obj = base_api.BaseAPI()
    t1 = time.perf_counter()
    for _ in range(1000000):
        ret = obj.readReg(base_api.REG76_ADDR)
        obj.writeReg(base_api.REG76_ADDR, ret)
    # print("0x%08x" %ret)
    t2 = time.perf_counter()
    # obj.writeReg(base_api.REG76_ADDR, 100)
    t3 = time.perf_counter()
    print(t2 - t1, t3 - t2)

def printReg(obj):
    for i in range(20):
        ret = obj.readReg(base_api.REG1_ADDR + i * 4)
        print('reg%d = 0x%08X' %(i + 1 , ret))

def test():
    obj = base_api.BaseAPI()
    obj.devInit()
    obj.selectChip(4)
    addr = base_api.AddrInfo()
    addr.colStart = 0
    addr.colCount = 8
    addr.rowStart = 0
    addr.rowCount = 1
    ret = obj.calcCells(addr)
    print(ret)
    t1 = time.perf_counter()
    for i in range(4):
        for j in range(8):
            ret = obj.calcOneCell_1(i, j)
            # ret = obj.readOneCell_1(i, j)
            print('%02d ' % ret, end='')

            # obj.resetOneCell_1(i, j, 2.5, 3.5, 200)
            # ret = obj.readOneCell_1(i, j)
            # print(ret)

            # obj.setOneCell_1(i, j, 2, 3, 200)
            # ret = obj.readOneCell_1(i, j)
            # print(ret)

            # obj.formOneCell_1(i, j, 4.55, 1.87, 10000)
            # ret = obj.readOneCell_1(i, j)
            # print(ret)
        print()
    t2 = time.perf_counter()
    print(t2 - t1)

def mapTest():
    obj = operation.Operation()
    obj.rramApi.devInit()
    obj.selectChip(0)
    addr = base_api.AddrInfo()
    addr.colStart = 0
    addr.colCount = 10
    addr.rowStart = 0
    addr.rowCount = 10
    mapPara = operation.MapPara()
    array = [[10 for _ in range(base_api.TOTAL_COL)] for _ in range(base_api.TOTAL_ROW)]
    obj.map(addr, mapPara, array)

def formTest():
    obj = base_api.BaseAPI()
    obj.devInit()
    obj.selectChip(0)
    for i in range(1152):
        for j in range(128):
            ret = obj.readOneCell_1(i, j)
            if ret < 9:
                obj.formOneCell_1(i, j, 4.55, 1.87, 1000000)

def readAllTest():
    obj = base_api.BaseAPI()
    obj.devInit()
    obj.selectChip(0)
    totalCell = 128 * 1152
    cnt = 0
    for i in range(1152):
        for j in range(128):
            ret = obj.readOneCell_1(i, j)
            if 9 <= ret <= 11:
                cnt += 1
    print('Accuracy = %f' % (cnt / totalCell))

import os
import ctypes
FILE_PATH = os.path.realpath('')
CLIB_PATH = FILE_PATH + '/lib_base_api.so'
def clibTest():
    obj = base_api.BaseAPI()
    obj.devInit()
    obj.selectChip(0)
    rowStart = 0
    rowCount = 1
    colStart = 0
    colCount = 128
    calcCount = 1

    rowInput = calcCount * rowCount * [1]
    rowInput = np.array(rowInput, dtype=np.uint8)
    rowInput.resize(calcCount, rowCount)
    t1 = time.perf_counter()
    ret = obj.calcArray_C(rowInput, rowStart, rowCount, colStart, colCount)
    t2 = time.perf_counter()
    print(t2 - t1)
    print(ret)
    print(ret.shape)

# formTest()
# mapTest()

clibTest()
